package crawl

import (
	"bytes"
	"encoding/json"
	"encoding/xml"
	"io"
	"net/url"
	"strings"
)

const (
	CharsetUTF8            = "utf-8"
	MimeNone               = ""
	MimeFormUrlencoded     = "application/x-www-form-urlencoded"
	MimeFormUrlencodedUTF8 = MimeFormUrlencoded + "; " + CharsetUTF8
	MimeJSON               = "application/json"
	MimeJSONUTF8           = MimeJSON + "; " + CharsetUTF8
	MimeXML                = "text/xml"
	MimeXMLUTF8            = MimeXML + "; " + CharsetUTF8
)

func FormInput(form url.Values) (io.Reader, string) {
	return strings.NewReader(form.Encode()), MimeFormUrlencodedUTF8
}

func JSONInput(input interface{}) (io.Reader, string) {
	v, _ := json.Marshal(input)
	return bytes.NewReader(v), MimeJSONUTF8
}

func XMLInput(xmlObj interface{}) (io.Reader, string) {
	v, _ := xml.Marshal(xmlObj)
	return bytes.NewReader(v), MimeXMLUTF8
}
